﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using Application.WebAPI.Filter;
using System.Web;

namespace Application.WebAPI.Controllers
{
    [RoutePrefix("api/test3")]
    public class ValuesController : BaseController
    {
        [AuthorizeFilter, Route("getname")]
        //public async Task<string> GetName()
        //{
        //	return commonGlobal.ReturnJson(200, LoginUserName());
        //}
        public HttpResponseMessage GetName()
        {
            return commonGlobal.ReturnJson(200, LoginUserName());
        }
    }
    [RoutePrefix("api/test2")]
    public class TestAController : BaseController
    {
        [Route("getname"), AllowAnonymous]
        public async Task<string> GetName()
        {
            //Framework.Cache.Redis.RedisBase redis = new Framework.Cache.Redis.RedisBase(new string[] { "123456@127.0.0.1:6379" }, new string[] { "123456@127.0.0.1:6379" });
            //bool flag = redis.Item_Set<string>("testItemSet", "哈哈");
            return "";
        }

        /// <summary>
        /// Post取值
        /// </summary>
        /// <param name="test"></param>
        /// <returns></returns>
        [Route("value1"), HttpPost, AllowAnonymous]
        public async Task<string> Value1([FromBody]test test)
        {
            return test.Name + " " + test.Age;
        }

        /// <summary>
        /// Get取值
        /// </summary>
        /// <param name="test"></param>
        /// <returns></returns>
        [Route("value2"), HttpGet, AllowAnonymous]
        public async Task<string> Value2([FromUri]test test)
        {
            return test.Name + " " + test.Age;
        }

        /// <summary>
        /// Get取值
        /// </summary>
        /// <param name="test"></param>
        /// <returns></returns>
        [Route("value3"), HttpGet, AllowAnonymous]
        public async Task<string> Value3(string name, int age)
        {
            return name + " " + age;
        }

        [Route("value4"), HttpGet, AllowAnonymous]
        public async Task<string> Value4()
        {
            //获取传统context  
            HttpContextBase context = (HttpContextBase)Request.Properties["MS_HttpContext"];

            //定义传统request对象
            HttpRequestBase request = context.Request;

            string name = request.QueryString["name"];
            return name;
        }

        [Route("getjson"), HttpGet, AllowAnonymous]
        public HttpResponseMessage ReturnJsonTest()
        {
            this.InitTestAccount();

            var json = commonGlobal.ReturnJson(200, "成功");
            //return JsonObject(json);
            return json;
        }



        private void InitTestAccount()
        {
            System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
            //client.DefaultRequestHeaders.Add("Content-Type", "application/x-www-form-urlencoded");

            List<KeyValuePair<string, string>> listkey = new List<KeyValuePair<string, string>>();
            listkey.Add(new KeyValuePair<string, string>(Provider.Paths.GrantType, Provider.Paths.GrantTypes.Password));
            listkey.Add(new KeyValuePair<string, string>(Provider.Paths.UserName, "test"));
            listkey.Add(new KeyValuePair<string, string>(Provider.Paths.Password, "test"));

            System.Net.Http.FormUrlEncodedContent content = new System.Net.Http.FormUrlEncodedContent(listkey);
            System.Threading.Tasks.Task<System.Net.Http.HttpResponseMessage> response = client.PostAsync(Provider.Paths.TokenUrl, content);
            System.Net.Http.HttpResponseMessage result = response.Result;
            System.Threading.Tasks.Task<string> token = result.Content.ReadAsStringAsync();
            string tokenStr = token.Result;
        }
    }

    public class test
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
}